{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch as t\n",
    "import sys\n",
    "sys.path.append('../')\n",
    "from utils import get_score\n",
    "import glob\n",
    "import torch as t \n",
    "import numpy as np\n",
    "import json\n",
    "import time\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "label_path =   '/home/a/code/pytorch/zhihu/ddd/labels.json'\n",
    "test_data_path='/home/a/code/pytorch/zhihu/ddd/test.npz' \n",
    "index2qid = np.load(test_data_path)['index2qid'].item()\n",
    "with open(label_path) as f: \n",
    "      labels_info = json.load(f)\n",
    "qid2label = labels_info['d']\n",
    "label2qid = labels_info['id2label']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data1=\"/data_ssd/zhihu/result/search_result/test_all_score.pth\"\n",
    "data2=\"/data_ssd/zhihu/result/search_result/test_all_first.pth\"\n",
    "result1=t.load(data1).float().topk(5,1)[1]\n",
    "result2=t.load(data2).float().topk(5,1)[1]\n",
    "\n",
    "result=t.load(data1).float().topk(5,1)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "212622\n"
     ]
    }
   ],
   "source": [
    "print t.sum(result2[:,0]==result[:,0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "def write_csv(result,index2qid,label2qid,filename):\n",
    "    #path=\"/data_ssd/zhihu/result/search_result/\"\n",
    "    f=open(filename+\".csv\", \"wa\")\n",
    "    csv_writer = csv.writer(f, dialect=\"excel\")\n",
    "    rows=[0 for _ in range(result.size(0))]\n",
    "    for i in range(result.size(0)):\n",
    "        tmp=result[i].sort(dim=0,descending=True)\n",
    "        tmp=tmp[1][:5]\n",
    "        row=[index2qid[i]]+[label2qid[str(int(i_))] for i_ in tmp]\n",
    "        if i%50000==0:\n",
    "            print row\n",
    "        rows[i]=row\n",
    "    csv_writer.writerows(rows)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'module' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-55-0586ce9a0502>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'module' object is not callable"
     ]
    }
   ],
   "source": [
    "t.from_numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def merge(file1,file2):\n",
    "    '''\n",
    "    file1:score\n",
    "    file2:first\n",
    "    '''\n",
    "    Num=file1.size(0)\n",
    "    res=t.zeros(file1.size())\n",
    "    for ii in range(Num):\n",
    "        tmp=file1[ii]\n",
    "        if file2[ii,0] not in tmp:\n",
    "            res[ii,0]=file2[ii,0]\n",
    "            res[ii,1:]=tmp[1:]\n",
    "        else:\n",
    "            if file1[ii,0]==file2[ii,0]:\n",
    "                res[ii]=file1[ii]\n",
    "            else:\n",
    "                res[ii,0]=file2[ii,0]\n",
    "                aaa=np.array([r for r in tmp if r!=file1[ii,0]])\n",
    "                res[ii,1:]=t.from_numpy(aaa)\n",
    "    return res\n",
    "                \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1422\n",
       "  808\n",
       "  714\n",
       " 1232\n",
       "  609\n",
       "[torch.LongTensor of size 5]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result2[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1422\n",
       "  808\n",
       " 1232\n",
       "  714\n",
       "  609\n",
       "[torch.FloatTensor of size 5]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rer[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([217360, 5])\n"
     ]
    }
   ],
   "source": [
    "print result.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[u'6215603645409872328', u'4610596224687453206', u'-7506384235581390893', u'2919247920214845195', u'-6839713564940654454', u'-5932391056759866388']\n",
      "[u'2558136278626495418', u'3040328401474530651', u'5947293336854386967', u'2587540952280802350', u'-2299947672181109599', u'6006627476560013656']\n",
      "[u'-5497959296411633559', u'-7057974542385626232', u'7263346613600732510', u'7592036674792056069', u'-8261875436896588399', u'-2244015987817045493']\n",
      "[u'8330621920012103679', u'2515257722796787281', u'6179724419779689236', u'1790630374868647951', u'7580468821069307570', u'-2523521411748733187']\n",
      "[u'909270500917796775', u'-1945278234078776687', u'521166281704794588', u'5953579394093512194', u'-6748914495015758455', u'7476760589625268543']\n"
     ]
    }
   ],
   "source": [
    "rows = range(rer.size(0))\n",
    "for ii,item in enumerate(rer):\n",
    "    rows[ii] = [index2qid[ii]] + [label2qid[str(int(_))] for _ in item ]\n",
    "    if ii%50000==0:\n",
    "        print rows[ii]\n",
    "import csv\n",
    "with open('0.43082.csv','w') as f:\n",
    "    writer = csv.writer(f)\n",
    "    writer.writerows(rows)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "212622\n"
     ]
    }
   ],
   "source": [
    "result[:,0]=result2[:,0]\n",
    "print t.sum(result1[:,0]==result[:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([217360])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "torch.Size([217360])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=rer[:,0]\n",
    "b=result[:,0]\n",
    "print a.size()\n",
    "b.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "true_labels = [qid2label[index2qid[2999967-200000+ii]] for ii in range(200000)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.43082848236815924"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(rer,true_labels)]\n",
    "score,_,_,_ = get_score(predict_label_and_marked_label_list)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/LSTMText0.4119_word_val.pth\n",
      "../checkpoints/result/RCNN_0.4037_char_val.pth\n",
      "../checkpoints/result/LSTMText0.4031_char_val.pth\n",
      "../checkpoints/result/RCNN_0.4115_word_val.pth\n",
      "../checkpoints/result/DeepText0.4103_word_val.pth\n",
      "../checkpoints/result/FastText3_word_0.409160_val.pth\n",
      "../checkpoints/result/CNNText_tmp0.4109_word_val.pth\n",
      "../checkpoints/result/CNNText_tmp0.4024_char_val.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/*val.pth')\n",
    "r2=0\n",
    "for file in files:\n",
    "    if 'MultiModel' not in file:\n",
    "        print file\n",
    "        r2+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "true_labels = [qid2label[index2qid[2999967-200000+ii]] for ii in range(len(r2))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.42763755264432574"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result=r2.topk(5,1)[1]\n",
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,_ = get_score(predict_label_and_marked_label_list)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "del r2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/DeepText_word_0.410051_aug_val.pth\n",
      "../checkpoints/result/tmp/RCNN0.41344_aug_word_val.pth\n",
      "../checkpoints/result/tmp/FastText3_word_weight1_0.40853_val.pth\n",
      "../checkpoints/result/tmp/inception0.41254_aug_word_val.pth\n",
      "../checkpoints/result/tmp/MultiCNNTextBNDeep_aug_char_0.38738_val.pth\n",
      "../checkpoints/result/tmp/RCNN0.39854_aug_char_val.pth\n",
      "../checkpoints/result/tmp/LSTMText0.41368_aug_word_val.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/tmp/*val.pth')\n",
    "r_aug=0\n",
    "for file in files:\n",
    "    if 'MultiModel' not in file and 'weight5' not in file:\n",
    "        print file\n",
    "        r_aug+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.42659117805840374"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result=r_aug.topk(5,1)[1]\n",
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,_ = get_score(predict_label_and_marked_label_list)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "del r_aug"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/MultiModelallfast_0.419088_aug_val.pth\n",
      "../checkpoints/result/tmp/MultiModel_0.4171859_val.pth\n",
      "../checkpoints/result/tmp/MultiModel2_0.42560_val.pth\n",
      "../checkpoints/result/tmp/MultiModel_0.42169_val.pth\n",
      "../checkpoints/result/tmp/MultiModelall_0.4235_aug_val.pth\n",
      "../checkpoints/result/tmp/MultiModel2w2c_0.4213_val.pth\n",
      "../checkpoints/result/tmp/MultiModelallfast_0.41652_aug_val.pth\n",
      "../checkpoints/result/tmp/MultiModel_0.41987_word_val.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/tmp/*val.pth')\n",
    "r_multi=0\n",
    "for file in files:\n",
    "    if 'MultiModel' in file :\n",
    "        print file\n",
    "        r_multi+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4302698214561717"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result=r_multi.topk(5,1)[1]\n",
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,_ = get_score(predict_label_and_marked_label_list)\n",
    "del r_multi\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/MultiCNNTextBNDeep_word_weight5_0.409409_val.pth\n",
      "../checkpoints/result/tmp/LSTMText_0.41230_weight5_val.pth\n",
      "../checkpoints/result/tmp/RCNN_0.41226_weight5_val.pth\n",
      "../checkpoints/result/tmp/inception_0.409912_weight5_val.pth\n",
      "../checkpoints/result/tmp/FastText3_word_weight5_0.407992_val.pth\n"
     ]
    }
   ],
   "source": [
    "files = glob.glob('../checkpoints/result/tmp/*val.pth')\n",
    "r_5=0\n",
    "for file in files:\n",
    "    if 'weight5' in file :\n",
    "        print file\n",
    "        r_5+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4187042397507684"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result=r_5.topk(5,1)[1]\n",
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,_ = get_score(predict_label_and_marked_label_list)\n",
    "del r_5\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['/data_ssd/zhihu/result/search_result/search_multimodel_result_score.pth',\n",
       " '/data_ssd/zhihu/result/search_result/search_nomulti_weight1_result_score.pth',\n",
       " '/data_ssd/zhihu/result/search_result/search_noaug_result_score.pth',\n",
       " '/data_ssd/zhihu/result/search_result/search_weight5_result_score.pth']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "files = glob.glob('/data_ssd/zhihu/result/search_result/*_score.pth')\n",
    "files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "r_re=18*t.load(files[0])+1.495*t.load(files[1])+t.load(files[2])+0.15*t.load(files[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.43059689877126467"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result=r_re.topk(5,1)[1]\n",
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,_ = get_score(predict_label_and_marked_label_list)\n",
    "del r_re\n",
    "score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "18 1.0 1 0.15    0.4305411700375018\n",
    "18 1.2 1 0.15    0.4305619687580149\n",
    "18 1.4 1 0.15    0.4305940768722144\n",
    "18 1.45 1 0.15   0.43059294166321205\n",
    "18 1.5 1 0.15    0.4306030584472225\n",
    "18 1.55 1 0.15   0.43060288677521397\n",
    "18 1.6 1 0.15    0.43059537396039643\n",
    "18 1.8 1 0.15    0.4305601537228334\n",
    "18 2.5 1 0.15    0.4304910145968311"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10.0 1.5 1 0.15    0.43052812976083576\n",
    "15.0 1.5 1 0.15    0.43058234950124885\n",
    "17.0 1.5 1 0.15    0.4306006464205759\n",
    "18.0 1.5 1 0.15    0.4306030584472225\n",
    "18.4 1.5 1 0.15    0.43059276943956926\n",
    "18.5 1.5 1 0.15    0.43059091975216024\n",
    "19.0 1.5 1 0.15    0.4305840952366161\n",
    "20.0 1.5 1 0.15    0.4305720334469683\n",
    "30.0 1.5 1 0.15    0.4305376536701278"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'r2' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-ac51f37684ba>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mresult\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr2\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mr_aug\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m1.1\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mr_multi\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m14\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mr_5\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m0.15\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtopk\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'r2' is not defined"
     ]
    }
   ],
   "source": [
    "result=(r2+r_aug*1.1+r_multi*14+r_5*0.15).topk(5,1)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4307485778331298"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,_ = get_score(predict_label_and_marked_label_list)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "    rows = range(result.size(0))\n",
    "    for ii,item in enumerate(result):\n",
    "        rows[ii] = [index2qid[ii]] + [label2qid[str(_)] for _ in item ]\n",
    "    import csv\n",
    "    with open('result0.43074.csv','w') as f:\n",
    "        writer = csv.writer(f)\n",
    "        writer.writerows(rows)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import csv\n",
    "line_no=217360\n",
    "rate = 0.003\n",
    "with open('result0.43074.csv') as f:\n",
    "    lines = [line.strip().split(',') for line in f.readlines()]\n",
    "    num = rate*line_no\n",
    "    stride = int(1/rate)\n",
    "    for line in lines[::stride]:\n",
    "        line[1:]=[-1,-1,-1,-1,-1]\n",
    "with open('result0.43074*0.997.csv','w') as f:\n",
    "    writer = csv.writer(f)\n",
    "    writer.writerows(lines)  \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['6215603645409872328',\n",
       " '4610596224687453206',\n",
       " '-7506384235581390893',\n",
       " '-6839713564940654454',\n",
       " '2919247920214845195',\n",
       " '-5932391056759866388\\r\\n']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lines[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.43381518936123437"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.429477037467622/0.99"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4285008976173052"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,_ = get_score(predict_label_and_marked_label_list)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def target(args):\n",
    "        w1,w2,w3 = args\n",
    "        r = a + b*w1 +c*w2 + d*w3\n",
    "        result = r.topk(5,1)[1]\n",
    "        predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "        score,_,_,rrrrrrr = get_score(predict_label_and_marked_label_list)\n",
    "        print (args,score,_)#list_space = [hp.uniform('a',0,1),hp.uniform('b',0,1)]\n",
    "        return -rrrrrrr[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "d=results[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.9034742719438373"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[-1].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4267781957592053"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,_ = get_score(predict_label_and_marked_label_list)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.42655971790607233"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4253965699709525"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#coding:utf8\n",
    "\n",
    "\n",
    "label_path =   '/mnt/7/zhihu/ieee_zhihu_cup/data/labels.json'\n",
    "test_data_path='/mnt/7/zhihu/ieee_zhihu_cup/data/test.npz' \n",
    "def ensamble(file1,file2,label_path=label_path,     test_data_path=test_data_path,result_csv=None):\n",
    "\n",
    "    if result_csv is None:\n",
    "        \n",
    "        result_csv = time.strftime('%y%m%d_%H%M%S.csv')\n",
    "    a = t.load(file1)\n",
    "    b = t.load(file2)\n",
    "    r = 9.0*a+b\n",
    "    result = r.topk(5,1)[1]\n",
    "    \n",
    "    index2qid = np.load(test_data_path)['index2qid'].item()\n",
    "    with open(label_path) as f:   label2qid = json.load(f)['id2label']\n",
    "    rows = range(result.size(0))\n",
    "    for ii,item in enumerate(result):\n",
    "        rows[ii] = [index2qid[ii]] + [label2qid[str(_)] for _ in item ]\n",
    "    import csv\n",
    "    with open(result_csv,'w') as f:\n",
    "        writer = csv.writer(f)\n",
    "        writer.writerows(rows)\n",
    "    \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    import fire\n",
    "    fire.Fire()\n",
    "\n",
    "files = ['CNNText_tmp0.4024_char_test.pth',\n",
    " 'CNNText_tmp0.4024_char_val.pth',\n",
    " 'DeepText0.4103_word_test.pth',\n",
    " 'Inception0.4110_word.pth',\n",
    " 'LSTMText0.4119_word.pth',\n",
    " 'LSTMText0.4031_char_test.pth',\n",
    " 'LSTMText0.4119_word_test.pth',\n",
    " 'DeepText0.4103_word_val.pth',\n",
    " 'CNNText_tmp0.4109_word_val.pth',\n",
    " 'LSTMText0.4119_word_val.pth',\n",
    " 'RCNN_0.4115_word_test.pth',\n",
    " 'RCNN_0.4037_char_val.pth',\n",
    " 'LSTMText0.4031_char_val.pth',\n",
    " 'RCNN_0.4115_word_val.pth',\n",
    " 'RCNN_0.4037_char_test.pth',\n",
    " 'CNNText_tmp0.4109_word_test.pth']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
